//
//  BLProgressHUD+private.m
//  BLUIKit
//
//  Created by macos on 2023/2/20.
//

#import "BLProgressHUD+private.h"
#import "MBProgressHUD.h"

@implementation BLProgressHUD (privatemethod)

#pragma mark - Private
+ (MBProgressHUD* _Nullable)showMsg:(NSString* _Nullable)msg toView:(UIView* _Nullable)toView autoHide:(BOOL)autoHide{
    if( msg.length == 0 ) return nil;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    hud.animationType = MBProgressHUDAnimationFade;
    hud.label.text = msg;
    
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    hud.label.textColor = [UIColor whiteColor];
    hud.removeFromSuperViewOnHide = true;
    if( autoHide ) {
        [hud hideAnimated:true afterDelay:1.5];
    }else{
        hud.mode = MBProgressHUDModeIndeterminate;
    }
    return hud;
}

+ (UIView*)getWindow{
    
    UIApplication *app = UIApplication.sharedApplication;
    
    NSArray *arr = app.connectedScenes.allObjects;
    for(NSUInteger i=0; i<arr.count; i++ ){
        UIScene *sec = arr[i];
        if( sec.activationState == UISceneActivationStateForegroundActive ) {
            if( [sec isKindOfClass:[UIWindowScene class]] ) {
                UIWindowScene *wsec = (UIWindowScene*)sec;
                NSArray *wins = wsec.windows;
                for(NSInteger j=0; j<wins.count; j++) {
                    UIWindow *w = wins[0];
                    if( w.isKeyWindow ) {
                        return w;
                    }
                }
            }
        }
    }
    
    return UIApplication.sharedApplication.windows.firstObject;
}

@end
